<?php
namespace app\api\controller;

use think\facade\Db;

class Login extends Common
{
    /**
     * @OA\Post(
     *     path="/api/login/index",
     *     tags={"登录接口"},
     *     summary="管理员登录",
     *     operationId="UserLogin",
     *     @OA\Parameter(
     *         name="username",
     *         in="query",
     *         description="管理员账号",
     *         required=true,
     *         @OA\Schema(
     *             type="string"
     *         )
     *     ),
     *     @OA\Parameter(
     *         name="password",
     *         in="query",
     *         description="密码",
     *         required=true,
     *         @OA\Schema(
     *             type="string"
     *         )
     *     ),
     *     security={
     *         {"petstore_auth": {"write:pets", "read:pets"}}
     *     },
     *     @OA\Response(
     *         response=200,
     *         description="登陆成功",
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             example={"code": 200,"result": "success","msg": "恭喜您登录成功！"},
     *         )
     *     ),
     *     @OA\Response(
     *         response=1000,
     *         description="用户名不能为空",
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             example={"code": 1000,"result": "fail","msg": "用户名不能为空"},
     *         )
     *     ),
     *     @OA\Response(
     *         response=1001,
     *         description="密码不能为空",
     *     ),
     *     @OA\Response(
     *         response=1002,
     *         description="用户不存在",
     *     ),
     *     @OA\Response(
     *         response=1003,
     *         description="密码错误",
     *     ),
     *     @OA\Response(
     *         response=1004,
     *         description="用户被禁用,请联系系统管理员！",
     *     ),
     *     @OA\Response(
     *         response=1005,
     *         description="用户所属角色组被禁用,请联系系统管理员！",
     *     ),
     *     @OA\Response(
     *         response=1006,
     *         description="用户所属角色组被删除,请联系系统管理员！",
     *     ),
     * )
     */
    public function index(){//print_r(json_encode($_REQUEST));exit;

        if(!$username = $this->request->request('username', ''))  return json(array('code'=>1000, 'result'=>'fail', 'msg'=>lang('userLogin.username')));
        if(!$password = $this->request->request('password', ''))  return json(array('code'=>1001, 'result'=>'fail', 'msg'=>lang('userLogin.password')));

        $resList = Db::name('user')->field('password, id, status, admintype, rid, cid')->where(['username'=>$username, 'is_delete'=>1])->find();

        if($resList){
            if($resList['status'] == 2)  return json(array('code'=>1004, 'result'=>'fail', 'msg'=>lang('userLogin.userProhibit')));

            //查询该用户所属角色是否被禁用
            $roleInfo = Db::name('role')->field('name, status')->where(['id'=>$resList['rid'] , 'is_delete'=>1])->find();
            if(!$roleInfo)  return json(array('code'=>1006, 'result'=>'fail', 'title'=>lang('userLogin.roleDel')));
            if($roleInfo['status'] == 2)  return json(array('code'=>1005, 'result'=>'fail', 'msg'=>lang('userLogin.roleProhibit')));

            $newPas = md5(md5($password));
            if($resList['password'] == $newPas){
                //登陆成功...
                return json(array('code'=>200, 'result'=>'success', 'msg'=>lang('userLogin.loginSucess')));
            }else{
                return json(array('code'=>1003, 'result'=>'fail', 'msg'=>lang('userLogin.passError')));
            }
        }else{
            return json(array('code'=>1002, 'result'=>'fail', 'msg'=>lang('userLogin.userNotExist')));
        }
    }
}
